<?php

class Ftp {

	private $handle;
	private $errorMessage;

	public function __construct($host, $port = 21, $timeout = 90) {
		$this->handle = ftp_connect($host, $port, $timeout);
		$this->errorMessage = '';
	}

	public function login($username, $password) {
		if (!ftp_login($this->handle, $username, $password)) {
			$this->errorMessage = 'login error';
			return false;
		}
		return true;
	}

	public function put($remoteFile, $localFile, $mode = FTP_BINARY, $startPosition = 0) {
		$result = ftp_nb_put($this->handle, $remoteFile, $localFile, $mode, $startPosition);

		while ($result == FTP_MOREDATA) {
			ftp_nb_continue($this->handle);
		}

		return $result == FTP_FINISHED;
	}

	public function del($remoteFile) {
		return ftp_delete($this->handle, $remoteFile);
	}

	public function errorMessage() {
		return $this->errorMessage;
	}

	public function __destruct() {
		ftp_close($this->handle);
	}

}
